package com.celan.year2023.month01.day20;

import java.util.*;

public class Solution {
    public int[] findingUsersActiveMinutes(int[][] logs, int k) {
        int[] ans = new int[k];
        //哈希表,key:id,value:去重后存储活跃时间
        HashMap<Integer, HashSet<Integer>> map = new HashMap<>();
        for (int i = 0; i < logs.length; i++) {
            int id = logs[i][0];
            int time = logs[i][1];
            if (map.containsKey(id)) {
                map.get(id).add(time);
            } else {
                HashSet<Integer> set = new HashSet<>();
                set.add(time);
                map.put(id, set);
            }
        }
        for (Map.Entry<Integer, HashSet<Integer>> entry : map.entrySet()) {
            int dym = entry.getValue().size();
            ans[dym - 1]++;
        }
        return ans;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode pA = headA, pB = headB;
        while (pA != pB) {
            pA = pA == null ? headB : pA.next;
            pB = pB == null ? headA : pB.next;
        }
        return pA;
    }

    public ListNode detectCycle(ListNode head) {
        if (head == null) return null;
        ListNode slow = head, fast = head;
        while (fast != null) {
            slow = slow.next;
            if (fast.next != null) {
                fast = fast.next.next;
            } else {
                return null;
            }
            if (slow == fast) {
                ListNode p = head;
                while (p != slow) {
                    p = p.next;
                    slow = slow.next;
                }
                return p;
            }
        }
        return null;
    }

    public String convert(String s, int numRows) {
        if(numRows < 2) return s;
        List<StringBuilder> rows = new ArrayList<>();
        //加入numRows行
        for(int i = 0; i < numRows; i++) rows.add(new StringBuilder());
        int i = 0, flag = -1;
        for(char c : s.toCharArray()) {
            rows.get(i).append(c);
            //如果行数向上加到0行，或者向下加到numRows - 1行，切换方向
            if(i == 0 || i == numRows - 1) flag = - flag;
            i += flag;
        }
        //最后将所有行的结果合并到一起
        StringBuilder res = new StringBuilder();
        for(StringBuilder row : rows) res.append(row);
        return res.toString();
    }

}